package com.xqb.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqb.common.utils.DateUtils;
import com.xqb.common.utils.SecurityUtils;
import com.xqb.modules.domain.po.LesCourse;
import com.xqb.modules.domain.po.LesCourseCatalogue;
import com.xqb.modules.domain.po.StdLearningLesson;
import com.xqb.modules.domain.vo.LastLearnVo;
import com.xqb.modules.mapper.LesCourseCatalogueMapper;
import com.xqb.modules.mapper.LesCourseMapper;
import com.xqb.modules.mapper.StdLearningLessonMapper;
import com.xqb.modules.service.IStdLearningLessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学生课程Service业务层处理
 * 
 * @author xqb
 * @date 2024-10-14
 */
@Service
public class StdLearningLessonServiceImpl implements IStdLearningLessonService
{
    @Autowired
    private StdLearningLessonMapper stdLearningLessonMapper;
    @Autowired
    private LesCourseMapper lesCourseMapper;
    @Autowired
    private LesCourseCatalogueMapper lesCourseCatalogueMapper;
    /**
     * 查询学生课程
     * 
     * @param id 学生课程主键
     * @return 学生课程
     */
    @Override
    public StdLearningLesson selectStdLearningLessonById(Long id)
    {
        return stdLearningLessonMapper.selectStdLearningLessonById(id);
    }

    /**
     * 查询学生课程列表
     * 
     * @param stdLearningLesson 学生课程
     * @return 学生课程
     */
    @Override
    public List<StdLearningLesson> selectStdLearningLessonList(StdLearningLesson stdLearningLesson)
    {
        return stdLearningLessonMapper.selectStdLearningLessonList(stdLearningLesson);
    }

    /**
     * 新增学生课程
     * 
     * @param stdLearningLesson 学生课程
     * @return 结果
     */
    @Override
    public int insertStdLearningLesson(StdLearningLesson stdLearningLesson)
    {
        stdLearningLesson.setCreateTime(DateUtils.getNowDate());
        return stdLearningLessonMapper.insertStdLearningLesson(stdLearningLesson);
    }

    /**
     * 修改学生课程
     * 
     * @param stdLearningLesson 学生课程
     * @return 结果
     */
    @Override
    public int updateStdLearningLesson(StdLearningLesson stdLearningLesson)
    {
        stdLearningLesson.setUpdateTime(DateUtils.getNowDate());
        return stdLearningLessonMapper.updateStdLearningLesson(stdLearningLesson);
    }

    /**
     * 批量删除学生课程
     * 
     * @param ids 需要删除的学生课程主键
     * @return 结果
     */
    @Override
    public int deleteStdLearningLessonByIds(Long[] ids)
    {
        return stdLearningLessonMapper.deleteStdLearningLessonByIds(ids);
    }

    /**
     * 删除学生课程信息
     * 
     * @param id 学生课程主键
     * @return 结果
     */
    @Override
    public int deleteStdLearningLessonById(Long id)
    {
        return stdLearningLessonMapper.deleteStdLearningLessonById(id);
    }
    /**
     * 查询最新学习信息
     * @return LastLearnVo
     */
    @Override
    public LastLearnVo selectLastLearnInfo() {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<StdLearningLesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StdLearningLesson::getUserId, userId);
        wrapper.orderByDesc(StdLearningLesson::getLatestLearnTime);
        StdLearningLesson stdLearningLesson = stdLearningLessonMapper.selectOne(wrapper);
        LastLearnVo lastLearnVo = new LastLearnVo();
        if (stdLearningLesson != null) {
            Long courseId = stdLearningLesson.getCourseId();
            Long latestSectionId = stdLearningLesson.getLatestSectionId();
            LesCourse lesCourse = lesCourseMapper.selectById(courseId);
            if (lesCourse != null) {
                LesCourseCatalogue lesCourseCatalogue = lesCourseCatalogueMapper.selectById(latestSectionId);
                if (lesCourseCatalogue != null) {
                    lastLearnVo.setLatestSectionName(lesCourseCatalogue.getName());
                    lastLearnVo.setCourseName(lesCourse.getName());
                    lastLearnVo.setCourseId(courseId);
                    lastLearnVo.setLatestLearnTime(stdLearningLesson.getLatestLearnTime());
                }
            }
        }
        return lastLearnVo;
    }
}
